#include "OLMenuPane.h"
#include "OLButton.h"
#include "OLAppTemplateDefs.h"
#include "OLApplication.h"

#include <QtGui/QPainter>
#include <QtGui/QVBoxLayout>
#include <QtCore/QDebug>
#include <QtGui/QGridLayout>

OLMenuPane::OLMenuPane(QWidget *parent) :
    QWidget(parent), m_opacity(1.0)
{
}

void OLMenuPane::paintEvent(QPaintEvent*)
{
    QPainter p(this);

    p.setOpacity(m_opacity);

    p.setBrush(Qt::NoBrush);
    p.setPen(OLStyles::buttonHighlightBorder());

    p.drawLine(0, height()-1, width(), height()-1);

    // draw background
//    p.setPen(Qt::white);
//    p.setBrush(QColor(0,0,0,200));
//    p.drawRoundedRect(rect().adjusted(0,0,-1,-1), 5, 5);

    // work out if a gridlayout is used or not
    bool isLandscape = false;
    QGridLayout* currentLayout = qobject_cast<QGridLayout*>(layout());
    if(currentLayout!=0)
        isLandscape = true;

    // draw separators between widgets
    p.setPen(QColor(33,33,33));
//    QList<OLButton*> widgets = findChildren<OLButton*>();
//    int childCount = widgets.count();

//    int bottomLineOffset = isLandscape ? (childCount+1)/2 : childCount;
//    --bottomLineOffset;
//    for(int ii=0; ii<bottomLineOffset; ++ii) {
//        OLButton* btn = widgets[ii];
//        if( btn->isVisible() ) {
//            int ypos = widgets[ii]->geometry().bottom() + 5;
//            p.drawLine(10, ypos, width()-10, ypos);
//        }
//    }

    if(isLandscape) {
        // draw separator thru middle
        p.drawLine(width()/2, 10, width()/2, height()-10);
    }

}

void OLMenuPane::addMenuItem(OLButton* menuitem)
{
    menuitem->setFixedHeight(FINGER_SIZE);
    menuitem->setParent(this);
}

void OLMenuPane::enableMenuItems()
{
    QList<OLButton*> menuItems = findChildren<OLButton*>();
    for(int ii=0; ii<menuItems.count(); ++ii) {
        menuItems[ii]->enable();
    }
}

void OLMenuPane::disableMenuItems()
{
    QList<OLButton*> menuItems = findChildren<OLButton*>();
    for(int ii=0; ii<menuItems.count(); ++ii) {
        menuItems[ii]->disable();
    }
}

void OLMenuPane::setOpacity(qreal opacity)
{
    m_opacity = opacity;
    update();
}

qreal OLMenuPane::opacity()
{
    return m_opacity;
}

void OLMenuPane::handlePortrait()
{
    QList<OLButton*> menuItems = findChildren<OLButton*>();
    delete layout();

    QVBoxLayout* newLayout = new QVBoxLayout(this);
    newLayout->setContentsMargins(10,10,10,10);
    newLayout->setSpacing(10);

    int btnCount = menuItems.count();
    for(int ii=0; ii<btnCount; ++ii) {
        newLayout->addWidget(menuItems[ii]);
    }

    setLayout(newLayout);
    resize(width(), minimumSizeHint().height());
}

void OLMenuPane::handleLandscape()
{
    QList<OLButton*> menuItems = findChildren<OLButton*>();
    delete layout();

    QGridLayout* newLayout = new QGridLayout(this);
    newLayout->setContentsMargins(10,10,10,10);
    newLayout->setSpacing(10);

    int btnCount = menuItems.count();
    int maxCol = (btnCount+1)/2;
    for(int ii=0; ii<btnCount; ++ii) {
        int col = ii/maxCol;
        int row = ii-(maxCol*col);
        newLayout->addWidget(menuItems[ii], row, col);
    }

    setLayout(newLayout);
    resize(width(), minimumSizeHint().height());
}


